package options

import (
	"github.com/spf13/pflag"
)

const (
	SlaveWriteEnabledKey = "sentinel.slave-write-enabled"
	AllEnabledKey        = "sentinel.all-enabled"
	WriteEnabledKey      = "sentinel.write-enabled"
	ReadEnabledKey       = "sentinel.read-enabled"
	SentinelRulesKey     = "sentinel.rules"

	// 各个 ResourceName 要和配置 SentinelOption.Rules#Resource 对齐
	WriteOpsResourceName      = "write-ops"
	ReadOpsResourceName       = "read-ops"
	AllOpsResourceName        = "all-ops"
	SlaveWriteOpsResourceName = "slave-write-ops"
)

type SentinelOption struct {
	SlaveWriteEnabled bool `mapstructure:"slave-write-enabled" json:"slave-write-enabled"`
	AllEnabled        bool `mapstructure:"all-enabled"         json:"all-enabled"`
	WriteEnabled      bool `mapstructure:"write-enabled"       json:"write-enabled"`
	ReadEnabled       bool `mapstructure:"read-enabled"       json:"read-enabled"`
	// Rules 限流规则, 是一个 github.com/alibaba/sentinel-golang/core/flow.Rule 类型的数组
	Rules string `mapstructure:"rules"    json:"rules"`
}

func NewSentinelOption() *SentinelOption {
	return &SentinelOption{}
}

func (opts *SentinelOption) AddFlags(fs *pflag.FlagSet) {
	fs.BoolVar(&opts.SlaveWriteEnabled, SlaveWriteEnabledKey, opts.SlaveWriteEnabled, "Specify the switch that whether to enable asyncRoute slave-write rate limiting.")
	fs.BoolVar(&opts.AllEnabled, AllEnabledKey, opts.AllEnabled, "Specify the switch that whether to enable all rate limiting.")
	fs.BoolVar(&opts.WriteEnabled, WriteEnabledKey, opts.WriteEnabled, "Specify the switch that whether to enable write-ops rate limiting.")
	fs.BoolVar(&opts.ReadEnabled, ReadEnabledKey, opts.ReadEnabled, "Specify the switch that whether to enable read-ops rate limiting.")
	fs.StringVar(&opts.Rules, SentinelRulesKey, opts.Rules, "Specify sentinel rate limiter rules.")
}

func (o *SentinelOption) Validate() []error {
	var errs = []error{}
	return errs
}
